package com.lx.util;

import jakarta.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 处理文件下载字符编码的工具类
 */
public class FileEncoderUtil {
    public static void FileEncoder(String fileName, HttpServletResponse response) throws UnsupportedEncodingException {
        String encodedHeader = "attachment; filename=\""
                + URLEncoder.encode(fileName, "UTF-8")
                + "\"; filename*=UTF-8''"
                + URLEncoder.encode(fileName, "UTF-8")
                .replace("+", "%20");

        // 设置响应内容类型
        response.setContentType("application/octet-stream");
        // 设置响应头
        response.setHeader("Content-Disposition", encodedHeader);

    }


}
